from socket import socket,AF_INET, SOCK_STREAM
from os import path as ospath
from os import popen as ospopen
import os
import platform
def Singleton(cls):
    _instance = {}
    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]
    return _singleton
def getconfig(url):
    c={}
    with open(url) as f:
        for i in f.readlines():
            if len(i)!=1:
                if i[0]!="#":
                    i=i.strip("\n")
                    i=i.split("=")
                    key = i[0].strip()
                    if len(i)==1:
                        c[key]=None
                    else:
                        c[key] = i[1].strip()
    return c
@Singleton
class config_imp():
    def __init__(self):
        self.mongodb_ip="127.0.0.1"
        self.factor_url=r"G:\factor\test"
        self.kline_url=r"G:\data"
        self.tick_url=r"G:\data"
        self.cpu_null_n=3
        self.load_config()
    def set_mongodb_ip(self,ip):
        self.mongodb_ip =ip
    def set_factor_url(self,url):
        self.factor_url=url
    def set_kline_url(self, url):
        self.kline_url = url
    def set_tick_url(self,url):
        self.tick_url=url
    def set_cpu_null_n(self,n):
        self.cpu_null_n=n
    def load_config(self,url="config.conf"):
        if ospath.exists(url):
            c=getconfig(url)
            for key,v in c.items():
                if "_url" in key:
                    self.__dict__[key]=ospath.abspath(v)
                elif "_num" in key:
                    self.__dict__[key] = float(v)
                elif "_int" in key:
                    self.__dict__[key] = int(float(v))
                else:
                    self.__dict__[key] = v

config=config_imp()
def telnet(ip,port):
    sock = socket(AF_INET, SOCK_STREAM)
    result = sock.connect_ex((ip,port))
    sock.close()
    if result == 0:
        return True
    else:
        return False
ismongo=False#telnet('127.0.0.1',27777)
ismongo_remote=False#telnet('127.0.0.1',27777)
ismongo_all=ismongo or ismongo_remote
isredis=False#telnet('127.0.0.1',6379)
def check_process(process_name):
    return len(ospopen('ps aux | grep "' + process_name + '" | grep -v grep').readlines())
def get_process(process_name):
    return ospopen('ps aux | grep "' + process_name + '" | grep -v grep').readlines()
def get_islocal():
    if ismongo:
        return True
    else:
        if ismongo_remote:
            return False
        else:
            return True
os_sys=platform.system()#Windows   Linux
def iswindowsfun():
    if os_sys=="Windows":
        return True
    else:
        return False
iswindows=iswindowsfun()
report_path=ospath.abspath(ospath.join(__file__, r"../../../reporthtml/"))
data_path=ospath.abspath(ospath.join(__file__, r"../../../data/"))
tick_path=ospath.abspath(ospath.join(__file__, r"../../../tick/"))
tick_url=tick_path
bar_url=ospath.abspath(ospath.join(__file__, r"../../../kline/"))
data_url=data_path
jili_url=ospath.abspath(ospath.join(__file__))
jili_url_pwd=os.getcwd()
jili_url0=ospath.abspath(r"../data/")